package com.caiya.service.impl;

import com.caiya.cart.dto.order.CartOrderListDto;
import com.caiya.cart.entity.CartOrder;
import com.caiya.cart.vo.order.CartOrderAddVo;
import com.caiya.cart.vo.order.CartOrderListVo;
import com.caiya.cart.vo.order.CartOrderUpdateVo;
import com.caiya.mapper.CartOrderMapper;
import com.caiya.mapper.CartServiceTypeMapper;
import com.caiya.service.CartOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2018/12/5 0005.
 */
@Service
public class CartOrderServiceImpl implements CartOrderService {

    @Autowired
    private CartOrderMapper cartOrderMapper;
    @Autowired
    private CartServiceTypeMapper cartServiceTypeMapper;

    @Override
    public PageInfo<CartOrderListDto> list(CartOrderListVo cartOrderListVo) {
        PageHelper.startPage(cartOrderListVo.getPageNum(),cartOrderListVo.getPageSize());
        List<CartOrderListDto> list = cartOrderMapper.list(cartOrderListVo);
        PageInfo<CartOrderListDto> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public int update(CartOrderUpdateVo cartOrderUpdateVo) {
        cartOrderUpdateVo.setCartServiceParentTypeName(cartServiceTypeMapper.selectNameById(Long.valueOf(cartOrderUpdateVo.getCartServiceParentType())));
        cartOrderUpdateVo.setCartServiceTypeName(cartServiceTypeMapper.selectNameById(Long.valueOf(cartOrderUpdateVo.getCartServiceType())));
        return cartOrderMapper.update(cartOrderUpdateVo);
    }

    @Override
    public int add(CartOrderAddVo cartOrderAddVo) {
        CartOrder order = new CartOrder();
        order.setUserId(Long.valueOf(cartOrderAddVo.getUserId()));
        order.setCartInfoId(Long.valueOf(cartOrderAddVo.getCartInfoId()));
        order.setCartServiceParentType(Long.valueOf(cartOrderAddVo.getCartServiceParentType()));
        order.setCartServiceType(Long.valueOf(cartOrderAddVo.getCartServiceType()));
        order.setCartServiceParentTypeName(cartServiceTypeMapper.selectNameById(Long.valueOf(cartOrderAddVo.getCartServiceParentType())));
        order.setCartServiceTypeName(cartServiceTypeMapper.selectNameById(Long.valueOf(cartOrderAddVo.getCartServiceType())));
        order.setOrderPrice(Long.valueOf(cartOrderAddVo.getOrderPrice()));
        order.setOrderRemark(cartOrderAddVo.getOrderRemark());
        order.setIsPay(cartOrderAddVo.getIsPay());
        return cartOrderMapper.insert(order);
    }
}
